home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_056 / mcad / mp / source / util.c < prev   
C/C++ Source or Header  |  1992-05-06  |  3KB  |  124 lines

  1. #include <ctype.h>
  2. #include <exec/types.h>
  3. #include "mp.h"
  4.  
  5. /*************************************************************
  6. * Round a positive integer to the "simplest" number in [val..limit]
  7. */
  8.  
  9. #define lsdig(x)  (((x) >= 10) ? (x)-((x)/10)*10 : (x))
  10.  
  11. iround(val,dir,limit)
  12. int val, dir;
  13. float limit;
  14.  
  15. {
  16.    int tmpval;
  17.  
  18.    switch (dir) {
  19.       case UP:
  20.          tmpval = lsdig(val);
  21.          tmpval = ( (tmpval) ? (val+10-tmpval) : val );
  22.          if (tmpval > limit) return(val);
  23.          if (val <= 10) return(tmpval);
  24.          val = iround(tmpval/10,dir,limit/10);
  25.          return(val*10);
  26.       case DOWN:
  27.          tmpval = val-lsdig(val);
  28.          if (tmpval < limit) return(val);
  29.          if (val <= 10) return(tmpval);
  30.          val = iround(tmpval/10,dir,limit/10);
  31.          return(val*10);
  32.    }   
  33.    return(val);
  34. }
  35.  
  36.  
  37. /*************************************************************
  38. * Round an FFP to the "simplest" number in [val..limit]
  39. */
  40.  
  41. #define FLARGE 1.0e18
  42.  
  43. FFP fround(ffpval, dir, ffplimit)
  44. FFP ffpval, ffplimit;
  45. int dir;
  46. {
  47.    float val, limit, fact=1.;
  48.    
  49.    val = ffpval;   
  50.    limit = ffplimit;   
  51.  
  52.    if (abs(val) < 1./FLARGE) return((FFP)0.);
  53.    if (val < 0.) {
  54.       val *= -1.; limit *= -1.; fact = -1.;
  55.       dir = (dir==UP ? DOWN : UP);
  56.    }
  57.    
  58.    while (val < 1.e5) {val*=10.; limit*=10.; fact*=10.;}
  59.    while (val > 1.e6) {val*=0.1; limit*=0.1; fact*=0.1;}
  60.    val = (float) iround( (int)val, dir, limit);
  61.    return( (FFP)(val/fact) );
  62. }
  63.  
  64.  
  65. /***************************************************************************/
  66. char *getwrd(bp)
  67. char **bp;
  68. {
  69.    char *retval;
  70.    
  71.    while (**bp == ' ') (*bp)++;
  72.    retval = *bp;
  73.    while (**bp && (**bp != ' ') ) (*bp)++;
  74.    return(retval);
  75. }
  76.  
  77.  
  78. /***************************************************************************/
  79. numeric(cp)
  80. char *cp;
  81. {
  82.    int rv = FALSE;
  83.    char c;
  84.    
  85.    while(*(cp) == ' ') cp++;
  86.  
  87.    while (c = *(cp++)) {
  88.       if (isdigit(c)||(toupper(c)=='E')||(c=='-')||(c=='+')||(c=='.'))
  89.          rv = TRUE;
  90.       else if ( c==' ')
  91.          return(rv);
  92.       else
  93.          return(FALSE);
  94.    }
  95.    return(rv);
  96. }
  97.  
  98.  
  99. /***************************************************************************/
  100. same(a,b,len)
  101. char *a, *b;
  102. int len;
  103. {
  104.    char i=0;
  105.    
  106.    while(*a && *b) {
  107.       if (toupper(*a) != toupper(*b)) return(FALSE);
  108.       a++; b++; i++;
  109.       if (i==len) return(TRUE);
  110.    }
  111.    return(i==len);
  112. }
  113.  
  114. /***************************************************************************/
  115. void trch(oldch,newch,buf)
  116. char oldch, newch, *buf;
  117. {
  118.    while (*buf) {
  119.       if (*buf == oldch) *buf = newch;
  120.       buf++;
  121.    }
  122. }
  123.  
  124.